-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
601 bookmarks initial libkiwix implementation #679
base: main
Are you sure you want to change the base?
Conversation
|
||
func testAddingBookmark() { | ||
let fileURLData = ZimFileService.getFileURLBookmarkData(for: testZimURL)! | ||
try! ZimFileService.shared.open(fileURLBookmark: fileURLData) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit cumbersome, we have a hash of stored Archive
under ZimFileService
that came from parsing things, using kiwix::library
mostly (in OPDSParser
), but we are not holding on to the library
.
In order for the bookmarks to be successfully added, we do need to add them to the library
itself (see here).
So we have a kind of dualism here, where the list of items are going through one temporary instance of library
to be parsed and inserted into the DB, whereas in order to bookmark them we need another instance of library
where the kiwix:Book
had to be added, otherwise the added kiwix::Bookmark
will be invalid
.
book.update(zim::Archive([fileURL fileSystemRepresentation])); | ||
} catch (std::exception e) { } | ||
return book; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a lot of legwork at the moment:
- we need a
kiwix::Book
to createkiwix::Bookmark
- we need a
kiwix::Archive
to createkiwix::Book
Whereas all we want is to store:
- 2 strings (title, description) a thumbnail URL, keyed by the article URL, which is more or less this much code:
struct SwiftBookmark {
let title: String
let description: String
let thumbnail: URL?
}
final class SwiftBookmarkManager {
private var dict: [URL: SwiftBookmark] = [:]
func add(bookmark: SwiftBookmark, withURL url: URL) {
dict[url] = bookmark
}
func remove(withURL url: URL) {
dict.removeValue(forKey: url)
}
func isBookmarked(withURL url: URL) -> Bool {
dict[url] != nil
}
}
This is to be compared with the 220 lines of code added in this PR...
- (BOOL) isBookmarked: (nonnull NSURL *) url inZIM: (nonnull NSUUID *) zimFileID { | ||
std::string url_c = [url fileSystemRepresentation]; | ||
std::string fileID_c = [[[zimFileID UUIDString] lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding]; | ||
const std::vector<kiwix::Bookmark> bookmarks = self.library->getBookmarks(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this call can be used without the argument and it works equally good:
self.library->getBookmarks();
The tricky part is, which is not much documented, is that the kiwix::Book
needs to be added to the library,
as on line 47.
If the Book
is not added to the library
, the self.library->getBookmarks();
won't return it, as it is an "invalid" bookmark..
The other solution is not to add the Book to the library and call getBookmarks(false)
.
Well... without looking into the C++ code it is not obvious...
|
||
XCTAssertTrue(bookmarks.isBookmarked(url: bookmarkURL)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For comparison testing the pure swift implementation, does the same at the moment (or even more as it supports thumbnail URL and description):
func testSwiftBookmars() {
let bookmarks = SwiftBookmarkManager()
XCTAssertFalse(bookmarks.isBookmarked(withURL: bookmarkURL))
let bookmark = SwiftBookmark(title: testTitle, description: "test snippet", thumbnail: nil)
bookmarks.add(bookmark: bookmark, withURL: bookmarkURL)
XCTAssertTrue(bookmarks.isBookmarked(withURL: bookmarkURL))
}
Plus currently the swift
code is out-performing the libkiwix
solution, mainly due to these extra bridging and casting (average: 0.000s vs. 0.013s)
Initial attempt to use libkiwix's bookmark functionality.
Not to be merged!
After some battle with the interoperability on 3 levels swift / objective-c / c++, this is an initial implementation I could do to re-use the LibKiwix bookmark functionality.
There are several issues with this approach, and several things are missing compared to what we currently have via CoreData (on device SQL DB):
kiwix::Bookmark
is not supporting "html snippet", and not supporting thumbnail url (see the screenshot below)As you can see on the screenshot, in order to list the Bookmarks, we need the following minimum data:
The above values are currently HTML parsed out from the Article, but the LibKiwix::Bookmark struct is not supporting these additional fields
At this stage I feel that going down this route we will add a lot of extra binding code, and we would need to re-implement most of the things the CoreData is offering out of the box.